/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of Game packet.
 *
 * Game packet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * Game packet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Game packet.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.game.ui;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class ScalableIconButton extends JButton implements ComponentListener {
	private static final long serialVersionUID = 4451079562958541714L;
	protected BufferedImage defaultIconImage;

	public ScalableIconButton(String name, String imagePath) {
		super(name);
		try {
			defaultIconImage = ImageIO.read(new File(imagePath));
			ImageIcon btnIcon = createImageIcon(defaultIconImage, name);
			if(btnIcon!=null)
			{
				this.setIcon(btnIcon);
			}
		} catch (Exception e) {
	        System.err.println("Couldn't create image: " + imagePath);
		}
		this.addComponentListener(this);
	}

	public ScalableIconButton(String name) {
		super(name);
		this.addComponentListener(this);
	}

	@Override
	public void componentHidden(ComponentEvent arg0) {
		
	}

	@Override
	public void componentMoved(ComponentEvent arg0) {
		
	}

	@Override
	public void componentResized(ComponentEvent e) {
		if(defaultIconImage!=null)
		{
			ImageIcon btnIcon = createImageIcon(defaultIconImage, this.getName());
			if(btnIcon!=null)
			{
				this.setIcon(btnIcon);
			}
		}
	}

	@Override
	public void componentShown(ComponentEvent arg0) {
		
	}
	
	/** Returns an ImageIcon, or null if the image was invalid. */
	protected ImageIcon createImageIcon(BufferedImage img, String description) {
		int width, height;
		try {
			if(img.getWidth()/this.getWidth() > img.getHeight()/this.getHeight()*0.7) {
				width = this.getWidth();
				height = -1;
			}
			else {
				width = -1;
				height = (int) (this.getHeight()*0.7);
			}
	        return new ImageIcon(img.getScaledInstance(width, height, 
	        		Image.SCALE_FAST), description);
		} catch (Exception e) {
	        System.err.println("Couldn't create icon: " + description);
			return null;
		}
	}
	
	public Dimension getTextMaxDimension()
	{
		int h, w;
		w = (int) (this.getWidth()*0.8);
		if(defaultIconImage!=null)
		{
			h = (int) (this.getHeight()*0.2);
		}
		else
		{
			h = (int) (this.getHeight()*0.5);
		}
		return new Dimension(w, h);
	}
}
